package com.tg.appcommon.android;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.Log;

import com.module.appcommon.R;

import java.util.Locale;

public class LanguageUtils {
    public static final String ZH_CN = "zh-cn";
    public static final String LANGUAGE_EN = "en";
    public static final String ENGLISH = "en-us";
    public static final String LANGUAGE_VI = "vi";
    public static boolean isZh(Context context) {
        return TextUtils.equals(getLanguage(context),ZH_CN);
    }

    public static boolean isEnglish(Context context){
        return TextUtils.equals(getLanguage(context),ENGLISH);
    }


    public static boolean isSimChinese(Context context) {
        String language = getLanguage(context);
        return ZH_CN.equals(language);
    }

    /**
     * 是否为简体或者繁体中文
     * @param context
     * @return
     */
    public static boolean isChinese(Context context) {
        String language = getLanguage(context);
        return language.startsWith("zh-");
    }

    public static String getCountry(Context context){
        Locale locale = context.getResources().getConfiguration().locale;
        return locale.getCountry();
    }

    public static String getLanguage(Context context) {
        return ResourcesUtil.getString(R.string.language);

    }

    public static void initAppLanguage(Context context,String lang) {
        Log.d("pigdreams", "配置语言...默认locale=" + Locale.getDefault());
        //Android 7.0之后需要用另一种方式更改res语言,即配置进context中
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             updateResources(context,lang);
        } else {
            //7.0之前的更新语言资源方式
            changeResLanguage(context,lang);
        }
        getCurrentLang();
    }

    private static Locale getCurrentLang() {
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage();
        Log.d("pigdreams", "配置语言...language=" + language);
        return Locale.TRADITIONAL_CHINESE;
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static void updateResources(Context context,String language) {
        Resources resources = context.getResources();
        Locale locale = new Locale(language);
        Configuration configuration = resources.getConfiguration();
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context.createConfigurationContext(configuration);
        resources.updateConfiguration(configuration,resources.getDisplayMetrics());
    }

    private static void changeResLanguage(Context context, String language) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = new Locale(language);
        configuration.setLocale(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }
}
